package com.example.demo_mvi

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.example.demo_mvi.intent.MyIntent
import com.example.demo_mvi.state.MyUIState
import com.example.demo_mvi.viewmodel.MyViewModel
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {
    private lateinit var myViewModel: MyViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        myViewModel = ViewModelProvider(this).get(MyViewModel::class.java)
        //4444444.订阅state，根据不同状态更新UI
        lifecycleScope.launch {
            myViewModel.uiState.collect{
                when(it){
                    is MyUIState.Success ->{
                        Log.d("ytx", "成功: "+it.videoList.size)
                    }
                    is MyUIState.Error ->{
                        Log.d("ytx", "失败了: "+it)
                    }
                    is MyUIState.Loading ->{
                        Log.d("ytx", "加载中: "+it)
                    }
                }
            }
        }

    }

    fun getData(view: View) {
        //1111111发送意图
        lifecycleScope.launch {
            myViewModel.newsChannel.send(MyIntent.GetVideos)
        }
    }
}